package com.cute4j.diffs.javaswing;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

/**
 * Simple Application.
 */
public class SimpleApplication extends JFrame {

  private final JTextArea textArea;

  public SimpleApplication() {
    super("Simple Text Viewer");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);
    JMenu fileMenu = new JMenu("File");
    menuBar.add(fileMenu);
    OpenAction openAction = new OpenAction();
    fileMenu.add(new JMenuItem(openAction));
    fileMenu.addSeparator();
    fileMenu.add(new JMenuItem(new ExitAction()));
    JToolBar toolBar = new JToolBar();
    toolBar.setRollover(true);
    getContentPane().add(toolBar, BorderLayout.NORTH);
    JButton openButton = new JButton(openAction);
    openButton.setText(null);
    toolBar.add(openButton);
    textArea = new JTextArea();
    getContentPane().add(new JScrollPane(textArea));
  }

  void loadFile(String fileName) {
    BufferedReader reader = null;
    try {
      PlainDocument document = new PlainDocument();
      FileReader fileReader = new FileReader(fileName);
      reader = new BufferedReader(fileReader);
      char[] buffer = new char[4096];
      int read;
      while ((read = fileReader.read(buffer, 0,
                                     buffer.length)) != -1) {
        document.
            insertString(document.getLength(),
                         new String(buffer, 0, read), null);
      }
      textArea.setDocument(document);
    } catch (BadLocationException ble) {
    } catch (IOException ioe) {
    } finally {
      if (reader != null) {
        try {
          reader.close();
        } catch (IOException ex) {
        }
      }
    }
  }

  class OpenAction extends AbstractAction {
    public OpenAction() {
      super("Open",
            new ImageIcon(
              SimpleApplication.class.
                getResource("../resources/OpenedFolder.png")));
      putValue(AbstractAction.SHORT_DESCRIPTION, "Open File");
    }
    public void actionPerformed(ActionEvent e) {
      JFileChooser fileChooser = new JFileChooser();
      if (fileChooser.showOpenDialog(SimpleApplication.this) ==
          JFileChooser.APPROVE_OPTION) {
        loadFile(fileChooser.getSelectedFile().
                                            getAbsolutePath());
      }
    }
  }

  static class ExitAction extends AbstractAction {
    public ExitAction() {
      super("Exit");
    }
    public void actionPerformed(ActionEvent e) {
      System.exit(0);
    }
  }

  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        try {
          UIManager.setLookAndFeel(
              UIManager.getSystemLookAndFeelClassName());
        } catch (Throwable t) {
        }
        SimpleApplication simpleApp = new SimpleApplication();
        simpleApp.setBounds(100, 100, 200, 170);
        simpleApp.setVisible(true);
      }
    });
  }

}
